import { _decorator, instantiate, Node, Prefab } from 'cc';
import { UIComponent } from '../../../../Framework/Scripts/Managers/UIComponent';
import { ResManager } from '../../../../Framework/Scripts/Managers/ResManager';
import { AbNames, EventMsgKes } from '../../Config/ResConfig';
import { GameStart1Item_Ctr } from './GameStart1Item_Ctr';
import { SceneMgr } from '../../../../Framework/Scripts/Managers/SceneMgr';
import { ThemeMatchRes } from '../../../AssetPackage/Props/Hall/Game/GameZT';


const { ccclass, property } = _decorator;

/**
 * 分类主题挑赛
 */
@ccclass('GameStart1_Ctr')
export class GameStart1_Ctr extends UIComponent {
    content: Node;
    protected onLoad(): void {
        this.InitUI();
    }

    start() {
        this.InitData();
    }

    protected onDestroy(): void {
        this.unRegisterEvent();
    }

    private InitUI(){
        this.registerEvent();
        this.setBgAni(0);
        this.content = this.ViewNode('ScrollView/view/content');

        this.AddButtonListener('btnBack',this,this.OnBtnCloseClick1);
    }

    private InitData(){
        // 请求主题赛配置
        globalThis.gameMgr._GameHallMsg.ThemeMatchReq();
    }

    private registerEvent(){
        globalThis.gameMgr._eventTarget.on(EventMsgKes.msgThemeMatchRes,this.onThemeMatchRes,this);
    }

    private unRegisterEvent(){
        globalThis.gameMgr._eventTarget.off(EventMsgKes.msgThemeMatchRes,this.onThemeMatchRes,this);
    }

    private OnBtnCloseClick1(){
        SceneMgr.inst.showHall();
    }

    //主题赛配置返回
    private onThemeMatchRes(data:ThemeMatchRes){
        console.log('=========onThemeMatchRes========');
        if(this.hasProInObj(data,'list')){
            let list = data.list;
            if(list){
                for (let i = 0; i < list.length; i++) {
                    let pre = ResManager.inst.getAsset(AbNames.GameHall,'Prefabs/GameStart1/GameStart1Item',Prefab);
                    if(pre){
                        let item:any = instantiate(pre);
                        this.content.addChild(item);
                        let ts_node:GameStart1Item_Ctr = item.addComponent(GameStart1Item_Ctr);
                        ts_node.setData(list[i]);
                    }
                }
            }
        }
    }   
}